#include <stdio.h>
// #include "peripheral_i2c.h"
#include "haptic_hv.h"

/**
  * @brief i2c read function
  * @param reg_addr: register address
  * @param reg_data: register data
  * @param len: number of read registers
  * @retval i2c read status: 0->success, 1->error
  */
//i0nt haptic_hv_i2c_reads(uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
//{
//	uint8_t cnt = 0;

//	while (cnt < AW_I2C_RETRIES) {
//		if (IIC_mem_read_bytes(reg_addr, reg_data, len))
//			return AW_SUCCESS;
//		cnt ++;
//	}
//	AW_LOGE("i2c read 0x%02X err!", reg_addr);
//	return AW_ERROR;
//}

/**
  * @brief i2c write function
  * @param reg_addr: register address
  * @param reg_data: register data
  * @param len: number of write registers
  * @retval i2c write status: 0->success, 1->error
  */
//i0nt haptic_hv_i2c_writes(uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
//{
//	uint8_t cnt = 0;

//	while (cnt < AW_I2C_RETRIES) {
//		if (IIC_mem_write_bytes(reg_addr, reg_data, len))
//			return AW_SUCCESS;
//		cnt ++;
//	}
//	AW_LOGE("i2c write 0x%02X err!", reg_addr);
//	return AW_ERROR;
//}

/**
  * @brief i2c write bits function, no need to modify
  * @param reg_addr: register address
  * @param reg_addr: register mask
  * @param reg_data: register data
  * @retval None
  */
void haptic_hv_i2c_write_bits(uint8_t reg_addr, uint8_t mask, uint8_t reg_data)
{
	uint8_t reg_val = 0;
	haptic_hv_mdelay(1);
	IIC_mem_read_bytes(reg_addr, &reg_val, AW_I2C_BYTE_ONE);
	reg_val &= mask;
	reg_val |= (reg_data & (~mask));
	haptic_hv_mdelay(1);
	IIC_mem_write_bytes(reg_addr, &reg_val, AW_I2C_BYTE_ONE);
	haptic_hv_mdelay(1);
}

/**
  * @brief stop hrtimer
  * @param None
  * @retval None
  */
void haptic_hv_stop_hrtimer(void)
{
	// TMR_Stop(TMR1);
}

/**
  * @brief start hrtimer
  * @param None
  * @retval None
  */
void haptic_hv_start_hrtimer(void)
{
	// TMR_Start(TMR1);
}

/**
  * @brief hrtimer callback function, it's used to long vibrator stop. should called by HAL_TIM_PeriodElapsedCallback
  * @param htim: hrtimer
  * @retval None
  */
void haptic_hv_tim_periodelapsedcallback()
{
		g_haptic_hv->timer_ms_cnt++;
		if (g_haptic_hv->timer_ms_cnt == g_haptic_hv->duration) {
			AW_LOGI("timer over, g_haptic_hv->duration:%d", g_haptic_hv->duration);
#ifdef AW_DOUBLE
			haptic_hv_change_motor(MOTOR_L);
			g_haptic_hv->duration = 0;
			g_haptic_hv->timer_ms_cnt = 0;
			aw8692x_play_stop();
			haptic_hv_change_motor(MOTOR_R);
			g_haptic_hv->duration = 0;
			g_haptic_hv->timer_ms_cnt = 0;
			aw8692x_play_stop();
			haptic_hv_stop_hrtimer();
#else
			g_haptic_hv->duration = 0;
			g_haptic_hv->timer_ms_cnt = 0;
			aw8692x_play_stop();
			haptic_hv_stop_hrtimer();
#endif
		}
}

/**
  * @brief delay function
  * @param ms: millisecond
  * @retval None
  */
void haptic_hv_mdelay(uint32_t ms)
{
	volatile uint16_t d;
	while(ms --){
		d = 512*8;
		while(d--);
	}
}

/**
  * @brief factory F0 calibration value can be stored in flash
  * @retval None
  */
void haptic_hv_set_cali_to_flash(void)
{
	AW_LOGI("f0 cali data is 0x%02x", g_haptic_hv->f0_cali_data);
}

/**
  * @brief update calibration values to driver
  * @retval None
  */
void haptic_hv_get_cali_from_flash(void)
{
	AW_LOGI("f0 cali data is 0x%02x", g_haptic_hv->f0_cali_data);
	/* g_haptic_hv->f0_cali_data = val; */
}

/**
  * @brief interrupt callback function, should called by HAL_GPIO_EXTI_Callback
  * @param GPIO_Pin: irq gpio pin
  * @retval None
  */
void haptic_hv_gpio_exti_callback(uint16_t GPIO_Pin)
{
	g_haptic_hv->irq_handle_flag = AW_IRQ_ON;
}

/**
  * @brief disable interrput gpio function
  * @param None
  * @retval None
  */
void haptic_hv_disable_irq(void)
{
	// HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
}

/**
  * @brief enable interrput gpio function
  * @param None
  * @retval None
  */
void haptic_hv_enable_irq(void)
{
	// HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}

/**
  * @brief GPIO setup functions.
  * @note Please implement it with your own platform, flexible selection of parameters.
  * @param GPIO_Pin  specifies the pin.
  * @param state  specifies the pin state.
  * @retval None
  */
void haptic_hv_set_reset_gpio(uint16_t GPIO_Pin, uint8_t state)
{
	// HAL_GPIO_WritePin(AW_RST_GPIO_Port, GPIO_Pin, state);
}
